<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<?php session_start(); ?>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link href="index.css" type="text/css" rel="stylesheet" media="screen" />
        <title></title>
    </head>
    <body>
        <div class="topbar">
            <div class="loginfo">
                <ul>
                    <li>
                        <?php
                        if(!isset($_SESSION["session_username"]))
                            {
                            ?>
                            <form action="login.php" method="post" onsubmit="return checkForm()">
                            用户名<input type = "text" name="username"/>
                            密码<input type = "password" name="password" /> 
                            <input type="submit" value="登陆" />
                        </form>
                        <input type="button" value="注册" onclick="window.location.href = 'RegisterSite.php'"></input>

                        <?php
                        }
                        else{
                            $name = $_SESSION["session_username"];
                            ?>
                            <?=$name?>
                            <?php
                            if(isset($_SESSION["session_admin"]) && $_SESSION["session_admin"] == true)
                            {
                            ?>
                            <input type="button" value="用户管理" onclick="window.location.href ='UserControl.php'"/>
                            <?php
                            }
                            ?>
                            <input type="button" value="注销" onclick="window.location.href ='Logout.php'"/>
                            <?php
                            }
                            ?>

                    </li>
                </ul>
            </div>
            <div class="globalitems">
                <ul>
                    <li>
                        <a href="index.php">首页</a>
                    </li>
                    <li>
                        <a href="SearchBook.php?allbook=true">书库</a>
                    </li>
                    <li>
                        <a href="showGroup.php">我的群组</a>
                    </li>
                    <li>
                        <a href="index.php">随便看看</a>
                    </li>    
                </ul>
            </div>
        </div>
        <div class="content">
            <div class="leftcontent">
        <?php
        // put your code here
        include_once 'User.php';
        include_once 'Note.php';
        include_once 'Group.php';
        if(!isset($_SESSION['session_username']))
        {
            header("location:RegisterSite.php");
            exit();
        }
        $name = $_SESSION['session_username'];
        $user = new NormalUser($name);
        $groupUserIn = $user->getGroups();
        foreach($groupUserIn as $group){
        ?>
            
                <div class="groupName">
                    <p><?=$group?></p>
                    <?php
                    $friends = $user->getFriends($group);
                    foreach($friends as $friend){
                        ?>
                    <div class="groupFriends">
                        <p><?=$friend?></p>
                        <?php
                        if($user->isTheAdmintrator($group)){
                        ?>
                            <form method="POST">
                                <input type="hidden" name="deletename" value="<?=$friend?>"/>
                                <input type="hidden" name="deletegroup" value="<?=$group?>"/>
                                <input type="submit" name="deletegroupmember" value="Delete <?=$friend?> in <?=$group?>"/>
                            </form>
                        <?php
                        }
                        if(isset($_POST['deletegroupmember'])){
                            deleteMember();
                        }
                        ?>
                        <?php
                        $friendsBooks = $user->getFriendBooks($friend);
                        foreach($friendsBooks as $friendsBook){
                        ?>
                        <div class="groupBooks">
                            <p>Notes about 《<span><?=$friendsBook?></span>》:</p>
                            <?php
                            $friendsNotes = $user->getFriendNotesOnBooks($friend,$friendsBook);
                            foreach($friendsNotes as $friendsNote){
                            ?>
                            <div class="groupNotes">
                                <p><?=$friendsNote?></p>
                                <form method="Post">
                                    <input type="text" sise="20" maxlength="100" name="content">
                                    <input name="bookname" value="<?=$friendsBook?>" type="hidden">
                                    <input name="username" value="<?=$name?>" type="hidden">
                                    <input type="submit" value="Add mynotes" name="addnotesubmit">
                                </form>
                            </div>
                            <?php
                                if(isset($_POST["addnotesubmit"])){
                                    addNotes();
                                    unset($_POST["addnotesubmit"]);
                                ?>
                                <script type="text/javascript"> 
                                        window.location.href = "showGroup.php"; 
                                </script> 
                                <?php
                                }
                            }
                            ?>
                        </div>
                        <?php
                        }
                        ?>
                        </div>
                    <?php
                    }
                    ?>
                </div>
            
        <?php
        }
        ?>
                </div>
        <div class="rightcontent">
            <div class="groupFriends">
                <div class="groupNotes">
                    <fieldset name="test">
                        <legend>New Note</legend>
                        <form method="POST">
                            书名<input type="text" name="bookname"/><br/>
                          	笔记<textarea name="content"/></textarea><br/>
                            <input type ="submit" value="submit" name="notesubmit"/>
                        </form>
                        <?php
                        if(isset($_POST["notesubmit"])){
                            createNote();
                            unset($_POST["notesubmit"]);
						?>
						<script type="text/javascript"> 
							window.location.href = "showGroup.php"; 
						</script> 
						<?php
                        }
                        ?>
                    </fieldset>
                </div>
            </div>
            <div class="groupFriends">
                <div class="groupNotes">
                    <fieldset>
                        <legend>New Group</legend>
                        <form method="POST">
                            组名<input type="text" name="groupname"/><br/>
                            <input type ="submit" value="submit" name="groupsubmit"/>
                        </form>
                        <?php
                        if(isset($_POST["groupsubmit"])){
                            createGroup();
                            unset($_POST["groupsubmit"]);
                            ?>
                            <script type="text/javascript"> 
                                    window.location.href = "showGroup.php"; 
                            </script> 
                            <?php

                        }
                        ?>
                    </fieldset>
                </div>
            </div>
            <div class="otherGroup">
        <?php
        $othergroups = $user->getGroupsNotIn();
        foreach($othergroups as $othergroup){
        ?>
        <div>
            <form method="POST">
                <?=$othergroup?>
                <input type="submit" value="Join the group" name="joinnewgroup">
                <input type="hidden" value="<?=$othergroup?>" name="groupname">
            </form>
        </div>
        <?php
        if(isset($_POST["joinnewgroup"])){
            joinNewGroup();
            unset($_POST["joinnewgroup"]);
		?>
		<script type="text/javascript"> 
			window.location.href = "showGroup.php"; 
		</script> 
		<?php
        }
        }
        ?>
        </div>
        </div>
        
    </div>
        <?php
    function addNotes(){
        $bookname = $_POST["bookname"];
        $username = $_POST["username"];
        $content = $_POST["content"];
        $currentTime = date("Y-m-d H:i:s");
        $query = "INSERT INTO notes (bookname,notesUser,content,tops,downs,time) value('".$bookname."','".$username."','".$content."',0,0,'$currentTime')";
        mysql_query($query);
        unset($_POST["addnotesubmit"]);
    }
    function createNote(){
        $bookname = $_POST["bookname"];
        $content = $_POST["content"];
        Note::newContent($bookname, $_SESSION['session_username'], $content);
        unset($_POST["notesubmit"]);
    }
    function createGroup(){
        $groupname = $_POST["groupname"];
        NormalGroup::createGroup($groupname,$_SESSION['session_username']);
        unset($_POST["groupsubmit"]);
    }
    function joinNewGroup(){
        $groupname = $_POST["groupname"];
        $find_administrator = "SELECT DISTINCT administrator FROM groups WHERE group_name = '".$groupname."'";
        $result_administrator = mysql_query($find_administrator);
        $administrator = mysql_fetch_array($result_administrator);
        $query = "INSERT INTO groups (username,group_name,administrator) value('".$_SESSION['session_username']."','".$groupname."','".$administrator['administrator']."')";
        mysql_query($query);
    }
    function deleteMember(){
        $name = $_POST['deletename'];
        $group = $_POST['deletegroup'];
        $query = "DELETE FROM groups WHERE group_name='".$group."' AND username='".$name."'";
        mysql_query($query);
    }
    ?>
    </body>
</html>